package com.magic.blog.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.magic.blog.entity.MCategory;
import com.magic.blog.service.MCategoryService;
import com.magic.blog.service.MPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * created by mzk on 2020/07/24
 */

@Component
public class ContextStartup implements ApplicationRunner , ServletContextAware {


    @Autowired
    private MCategoryService categoryService;


    ServletContext servletContext;

    @Autowired
    private MPostService postService;
    /**
     * 项目启动调用
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<MCategory> categories = categoryService.list(new QueryWrapper<MCategory>()
                .eq("status", 0)
        );
        servletContext.setAttribute("categorys",categories);

        postService.initWeekRank();
    }

    /**
     * 注入到应用级别
     * @param servletContext
     */
    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}
